//
//  PCDApiDownloadFile.m
//  PCDMPaasBank
//
//  Created by DK on 2021/3/8.
//  Copyright © 2021 Alibaba. All rights reserved.
//

#import "PCDApiDownloadFile.h"
#import "PCDBankWebViewController.h"
@implementation PCDApiDownloadFile

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
    NSDictionary *dic = (NSDictionary *)data;
    
    NSString *strUrl = [dic objectForKey:@"url"];
    NSString *callBack = [dic objectForKey:@"success"];
    NSString *failkcallBack = [dic objectForKey:@"fail"];
    
    if ([PCDUtil nilOrEmptyString:strUrl]) {
        return;
    }
    
    PCDFileDownloadTask *task =[[PCDFileDownloadTask alloc]initWithURLString:strUrl cachePath:nil];
    
    [task downloadCallback:^(NSData * _Nullable fileData, NSString * _Nullable fileMD5, NSError * _Nullable error) {
        if (!error && fileData) {
            //写文件
            NSString *zipFoler = [PCDSkinModel subFolderWithPath:kSkinZipFolder];
            NSString *zipPath = [zipFoler stringByAppendingPathComponent:[strUrl lastPathComponent]];
            [fileData writeToFile:zipPath atomically:YES];
            evaluateJavaScriptOnMainThread(self,callBack,@"");
        }else{
            evaluateJavaScriptOnMainThread(self,failkcallBack,@"");
        }
    }];
    
//    NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:@{
//                @"url":strUrl}];
//
//
//
//    PCDBankWebViewController *viewController= (PCDBankWebViewController *)[[CTMediator sharedInstance] CTMediatorH5ContainerViewController:paramsDic];
//
//    [vc.navigationController pushViewController:viewController animated:YES];
    
}

@end
